'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac0206
    Inherits Snac

    Public Sub New()
        MyBase.New(&H2, &H6)
    End Sub

    Private _Uin As String
    Public Property Uin() As String
        Get
            Return _Uin
        End Get
        Set(ByVal value As String)
            _Uin = value
        End Set
    End Property

    Private _WarningLevel As Integer
    Public Property WarningLevel() As Integer
        Get
            Return _WarningLevel
        End Get
        Set(ByVal value As Integer)
            _WarningLevel = value
        End Set
    End Property

    Private _UserClass As New TlvUserClass
    Public ReadOnly Property UserClass() As TlvUserClass
        Get
            Return _UserClass
        End Get
    End Property

    Private _UserStatus As New TlvUserStatus
    Public ReadOnly Property UserStatus() As TlvUserStatus
        Get
            Return _UserStatus
        End Get
    End Property

    Private _DCInfo As New TlvDCInfo
    Public ReadOnly Property DCInfo() As TlvDCInfo
        Get
            Return _DCInfo
        End Get
    End Property

    Private _ExternalAddress As New TlvExternalIpAddress
    Public ReadOnly Property ExternalAddress() As TlvExternalIpAddress
        Get
            Return _ExternalAddress
        End Get
    End Property

    Private _ClientIdleTime As New TlvClientIdleTime
    Public ReadOnly Property ClientIdleTime() As TlvClientIdleTime
        Get
            Return _ClientIdleTime
        End Get
    End Property

    Private _SignOnTime As New TlvSignonTime
    Public ReadOnly Property SignOnTime() As TlvSignonTime
        Get
            Return _SignOnTime
        End Get
    End Property

    Private _MemberSince As New TlvMemberSince
    Public ReadOnly Property MemberSince() As TlvMemberSince
        Get
            Return _MemberSince
        End Get
    End Property

    Private _EncodingType As New TlvEncodingType
    Public ReadOnly Property EncodingType() As TlvEncodingType
        Get
            Return _EncodingType
        End Get
    End Property

    Private _ClientProfileString As New TlvClientProfileString
    Public ReadOnly Property ClientProfileString() As TlvClientProfileString
        Get
            Return _ClientProfileString
        End Get
    End Property

    Private _AwayMessageEncoding As New TlvAwayMessageEncoding
    Public ReadOnly Property AwayMessageEncoding() As TlvAwayMessageEncoding
        Get
            Return _AwayMessageEncoding
        End Get
    End Property

    Private _AwayMessage As New TlvAwayMessageString
    Public ReadOnly Property AwayMessage() As TlvAwayMessageString
        Get
            Return _AwayMessage
        End Get
    End Property

    Private _UserCapabilities As New TlvCapabilities
    Public ReadOnly Property UserCapabilities() As TlvCapabilities
        Get
            Return _UserCapabilities
        End Get
    End Property


    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Snac.SizeFixPart

        Dim innerTlvCount As Integer
        Dim innerTlvIndex As Integer

        _Uin = ByteConverter.ToStringFromByteIndex(index, data)
        index += 1 + _Uin.Length

        _WarningLevel = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2

        innerTlvCount = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2

        Do While innerTlvIndex < innerTlvCount
            Dim desc As TlvDescriptor = TlvDescriptor.GetDescriptor(index, data)

            Select Case desc.TypeId
                Case &H1
                    _UserClass.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H6
                    _UserStatus.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &HC
                    _DCInfo.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &HA
                    _ExternalAddress.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &HF
                    _ClientIdleTime.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H3
                    _SignOnTime.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H5
                    _MemberSince.Deserialize(data.GetRange(index, desc.TotalSize))
            End Select

            index += desc.TotalSize
            innerTlvIndex += 1
        Loop

        Do While index < data.Count
            Dim desc As TlvDescriptor = TlvDescriptor.GetDescriptor(index, data)

            Select Case desc.TypeId
                Case &H1
                    _EncodingType.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H2
                    _ClientProfileString.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H3
                    _AwayMessageEncoding.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H4
                    _AwayMessage.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H5
                    _UserCapabilities.Deserialize(data.GetRange(index, desc.TotalSize))
            End Select

            index += desc.TotalSize
        Loop

        SetTotalSize(index)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Throw New NotImplementedException
    End Function
End Class

Public Class TlvEncodingType
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H1)
    End Sub

    Private _EncodingType As String
    Public Property EncodingType() As String
        Get
            Return _EncodingType
        End Get
        Set(ByVal value As String)
            _EncodingType = value
        End Set
    End Property


    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _EncodingType = ByteConverter.ToStringFromUInt16Index(index, data)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_EncodingType.Length)))
        data.AddRange(ByteConverter.GetBytes(_EncodingType))

        Return data
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        Return 2 + _EncodingType.Length
    End Function
End Class

Public Class TlvClientProfileString
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H2)
    End Sub

    Private _ClientProfile As String
    Public Property ClientProfile() As String
        Get
            Return _ClientProfile
        End Get
        Set(ByVal value As String)
            _ClientProfile = value
        End Set
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _ClientProfile = ByteConverter.ToStringFromUInt16Index(index, data)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_ClientProfile.Length)))
        data.AddRange(ByteConverter.GetBytes(_ClientProfile))

        Return data
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        Return 2 + _ClientProfile.Length
    End Function
End Class

Public Class TlvAwayMessageEncoding
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H3)
    End Sub

    Private _AwayMessageEncoding As String
    Public Property AwayMessageEncoding() As String
        Get
            Return _AwayMessageEncoding
        End Get
        Set(ByVal value As String)
            _AwayMessageEncoding = value
        End Set
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _AwayMessageEncoding = ByteConverter.ToStringFromUInt16Index(index, data)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_AwayMessageEncoding.Length)))
        data.AddRange(ByteConverter.GetBytes(_AwayMessageEncoding))

        Return data
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        Return 2 + _AwayMessageEncoding.Length
    End Function
End Class

Public Class TlvAwayMessageString
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H4)
    End Sub

    Private _AwayMessage As String
    Public Property AwayMessage() As String
        Get
            Return _AwayMessage
        End Get
        Set(ByVal value As String)
            _AwayMessage = value
        End Set
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _AwayMessage = ByteConverter.ToStringFromUInt16Index(index, data)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_AwayMessage.Length)))
        data.AddRange(ByteConverter.GetBytes(_AwayMessage))

        Return data
    End Function

    Public Overrides Function CalculateDataSize() As Integer
        Return 2 + _AwayMessage.Length
    End Function
End Class

'TODO: Complete Implementation of Location Services to achieve 100% AIM Support